package demo4_ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    ArrayList<Account> accounts = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    Random r = new Random();

    public String getAccountNumber(){
        String s = "";
        s += r.nextInt(9) + 1;
        for (int i = 0; i < 11; i++){
            s += r.nextInt(10);
        }
        return s;
    }
    private Account findAccountByNumber(){
        Account account = null;
        System.out.println("请输入卡号：");
        String number = sc.next();
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getNumber().equals(number)){
                account = accounts.get(i);

            }
        }
        return account;
    }
    private void addAccount(){
        Account account = new Account();
        System.out.println("====== 开户 ======");
        System.out.println("请输入用户名：");
        String name = sc.next();
        account.setName(name);
        while (true) {
            System.out.println("请输入密码：");
            String password = sc.next();
            System.out.println("请确定密码：");
            String password2 = sc.next();
            if (password.equals(password2)){
                account.setPassword(password);
                break;
            }
            else{
                System.out.println("你输入的俩次密码不一样，请重新输入~");
            }
        }
        System.out.println("请输入性别：");
        char gender = sc.next().charAt(0);
        account.setGender(gender);
        System.out.println("请输入限额：");
        double limit = sc.nextDouble();
        account.setLimit(limit);
        String number = getAccountNumber();
        account.setNumber(number);
        accounts.add(account);
        System.out.println("恭喜" + name + "开户成功,您的卡号是：" + number);

    }

    private Account loginAccount(){
        Account account = null;
        if (accounts.size() == 0){
            System.out.println("当前系统重没有用户，请先开户了，再登录~");
        }
        else {
            account = findAccountByNumber();
            if (account != null) {
                System.out.println("请输入密码：");
                int count = 3;
                while (true) {
                    String password = sc.next();
                    if (account.getPassword().equals(password)){
                        System.out.println("====== 登录成功 ======");
                        return account;
                    }
                    else {
                        count--;
                        System.out.println("登录失败,请重新输入密码(你还有" + count + "机会):");
                    }
                    if (count == 0){
                        System.out.println("====== 登录失败 ======");
                        break;
                    }
                }

            }
            else {
                System.out.println("你输入的卡号有误~~~");
            }

        }
        return account;
    }
    private void showAccount(Account account){
        System.out.println("====== 账户信息 ======");
        System.out.println("卡号：" + account.getNumber());
        System.out.println("用户名：" + account.getName());
        System.out.println("性别：" + account.getGender());
        System.out.println("余额：" + account.getMoney());
        System.out.println("限额：" + account.getLimit());
        System.out.println("====== 账户信息 ======");


    }
    private void addMoney(Account account){
        System.out.println("====== 存款 ======");
        while (true) {
            System.out.println("请输入存款金额：");
            double money = sc.nextDouble();
            if (money <= account.getLimit()){
                account.setMoney(account.getMoney() + money);
                System.out.println("存款成功，余额为：" + account.getMoney());
                System.out.println("====== 存款 ======");
                return;
            }
             else {
                System.out.println("存款失败,你的额度只有：" + account.getLimit());
            }
        }
    }
    private void subMoney(Account account){
        System.out.println("====== 取款 ======");
        while (true) {
            System.out.println("请输入取款金额：");
            double money = sc.nextDouble();
            if (money <= account.getMoney()){
                if (money <= account.getLimit()){
                    account.setMoney(account.getMoney() - money);
                    System.out.println("取款成功，余额为：" + account.getMoney());
                    System.out.println("====== 取款 ======");
                    return;
                }
                else {
                    System.out.println("取款失败,你的额度只有：" + account.getLimit());
                }
            }
            else {
                System.out.println("取款失败，当前余额不足！！！");
                System.out.println("当前的余额为：" + account.getMoney());
            }
        }
    }

    private void transferMoney(Account account){
        System.out.println("====== 转账 ======");
        if (accounts.size() >= 2){
            while (true) {
                Account account1 = findAccountByNumber();
                if (account1 != null){
                    while (true) {
                        System.out.println("请输入转账金额：");
                        double money = sc.nextDouble();
                        if (money <= account.getMoney()){
                            if (money <= account.getLimit()){
                                account.setMoney(account.getMoney() - money);
                                account1.setMoney(account1.getMoney() + money);
                                System.out.println("转入成功，当前账户余额为：" + account.getMoney());
                                System.out.println("====== 转账 ======");
                                return;
                            }
                            else {
                                System.out.println("转账金额超过最大转账额度(" + account.getLimit() + ")!!!");
                            }
                        }
                        else {
                            System.out.println("你当前的余额不足，余额为：" + account.getMoney());
                        }
                    }
                }
                else {
                    System.out.println("输入的卡号不存在!!!");
                }
            }
        }
        else {
            System.out.println("当前系统中，账户不足两个，请先创建！！！");
        }
        System.out.println("====== 转账 ======");

    }
    private void removeAccount(Account account){
        System.out.println("是否确定销户（y/n）");
        char command = sc.next().charAt(0);
        switch (command){
            case 'y':
                accounts.remove(account);
                System.out.println("销户成功~~~");
                break;
            default:
                System.out.println("取消销户成功~~~");
        }

    }

    private void accountOperation(Account account){
        while (true) {
            System.out.println("请您选择要操作的选项：");
            System.out.println("====== 1.查询账户信息 ======");
            System.out.println("====== 2.存款 ======");
            System.out.println("====== 3.取款 ======");
            System.out.println("====== 4.转账 ======");
            System.out.println("====== 5.销户 ======");
            System.out.println("====== 6.退出 ======");
            int command = sc.nextInt();
            switch(command){
                case 1:
                    showAccount(account);
                    break;
                case 2:
                    addMoney(account);
                    break;
                case 3:
                    subMoney(account);
                    break;
                case 4:
                    transferMoney(account);
                    break;
                case 5:
                    removeAccount(account);
                    return;
                case 6:
                    return;
                default :
                    System.out.println("输入错误，请重新输入~");
            }
        }
    }



    public void start(){
        while (true) {
            System.out.println("请您选择要操作的选项：");
            System.out.println("====== 1.开户 ======");
            System.out.println("====== 2.登录 ======");
            System.out.println("====== 3.退出 ======");
            int command = sc.nextInt();
            switch(command){
                case 1:
                    addAccount();
                    break;
                case 2:
                    Account account = loginAccount();
                    if (account != null) accountOperation(account);
                    break;
                case 3:
                    System.out.println("====== 退出成功 ======");
                    return;
                default :
                    System.out.println("输入错误，请重新输入~");

            }
        }
    }
}
